package ru.rchervot.sanasar.client.view;




import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widgetideas.client.CollapsiblePanel;

public class ModuleCandidatesView extends Composite implements ModuleSearchPresenterDisplay {
	
	private final ScrollPanel panelSections = new ScrollPanel();
	private final FlowPanel basePanel = new FlowPanel();
	private final FlowPanel contentPanel = new FlowPanel();
	private final Grid gridPanel = new Grid(1,2);
	
	private ToggleButton controlButton;
	private final CollapsiblePanel collapsiblePanel = new CollapsiblePanel();
	
	private final Widget sectionsDisplay;
	
	public ModuleCandidatesView(
			Widget moduleHeaderDisplay,
			Widget messagesDisplay,
			Widget sectionsDisplay) {
		
		this.sectionsDisplay = sectionsDisplay;
		
		basePanel.add(moduleHeaderDisplay);
		basePanel.add(messagesDisplay);
		basePanel.add(gridPanel);
		
		controlButton = new ToggleButton("click to pin", "click to collapse");
		controlButton.setStyleName("CollapsibleToggle");
		
		gridPanel.setSize("100%", "100%");
  	
		gridPanel.setCellPadding(0);
  	gridPanel.setCellSpacing(0);
  	
  	gridPanel.getCellFormatter().setWidth(0, 0, "1%");
  	gridPanel.getCellFormatter().setWidth(0, 1, "99%");
  	
  	gridPanel.setWidget(0, 1, contentPanel);
		
		
		
	//window resize handler 
		Window.addResizeHandler(new ResizeHandler() { 
			public void onResize(ResizeEvent event) { 
				onWindowResized(Window.getClientWidth(), Window.getClientHeight());
			} 
		}); 
		
		this.initWidget(basePanel);
		this.setWidth("100%");
	}
	
	public void attachCollapsible() {
		gridPanel.setWidget(0, 0, collapsiblePanel);
		gridPanel.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_BOTTOM);
		
		collapsiblePanel.setHoverBarContents(createHoverBarContent());
		collapsiblePanel.add(createCollapsibleContent());
		collapsiblePanel.setWidth(Constants.kSectionsWidth + "px");
		collapsiblePanel.hookupControlToggle(controlButton);
		collapsiblePanel.setCollapsedState(true);
	}
	
	private Widget createHoverBarContent() {
		Label hoverBar = new Label("»");
		hoverBar.setStyleName("ModuleSearchView_HoverBar");
		
		return hoverBar;
	}
	
  private Widget createCollapsibleContent() {
		panelSections.setSize(Constants.kSectionsWidth + "px", "100%");		    
		panelSections.add(sectionsDisplay);
		
		FlowPanel flowPanel = new FlowPanel();
		flowPanel.add(getScrollerPanelHeader());
		flowPanel.add(panelSections);
		
		return flowPanel;
  }

	private HorizontalPanel getScrollerPanelHeader() {
		HorizontalPanel panelHeader = new HorizontalPanel();
		
		panelHeader.setWidth("100%");
		panelHeader.setStyleName("nav-Tree-title");

		panelHeader.add(controlButton);
		panelHeader.setCellHeight(controlButton, Constants.SCROLLER_PANEL_HEADER_HEIGHT + "px");
//		panelHeader.setCellWidth(controlButton, "1px");
		panelHeader.setCellVerticalAlignment(controlButton,
				HasVerticalAlignment.ALIGN_MIDDLE);
		panelHeader.setCellHorizontalAlignment(controlButton, 
				HasHorizontalAlignment.ALIGN_CENTER);
		
		return panelHeader;
	}
	
	@Override
	protected void onLoad() {
		super.onLoad();
		
		attachCollapsible(); //Should be done here, because CollapsiblePanel only could be attached to already attached element
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}
	
	public void onWindowResized(int width, int height) {
		int scrollerHeightSections = height
		- Constants.kHeaderHeight
		- Constants.kModuleHeaderHeight
		- Constants.kMessagesHeight
		- Constants.SCROLLER_PANEL_HEADER_HEIGHT;
		
		if (scrollerHeightSections < 1) scrollerHeightSections = 1; //TODO 1px - really!?
		panelSections.setHeight(scrollerHeightSections + "px");
		
		int scrollerHeightCollapsible = height
		- Constants.kHeaderHeight
		- Constants.kModuleHeaderHeight
		- Constants.kMessagesHeight;
		
		if (scrollerHeightCollapsible < 1) scrollerHeightCollapsible = 1; //TODO 1px - really!?
		collapsiblePanel.setHeight(scrollerHeightCollapsible + "px");
		gridPanel.setHeight(scrollerHeightCollapsible + "px");
		gridPanel.getCellFormatter().setHeight(0, 0, scrollerHeightCollapsible + "px");
		gridPanel.getCellFormatter().setHeight(0, 1, scrollerHeightCollapsible + "px");
	}

	protected Presenter presenter;

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setContent(Presenter.Display contentDisplay) {
		Widget w = (Widget)contentDisplay;
		
		if (! w.isAttached()) {
			contentPanel.add(w);
		}
		
		showContent(w);
	}
	
	private void showContent(Widget w) {
		for (Widget widget : contentPanel) {
			if (widget == w) {
				widget.setVisible(true);
			} else {
				widget.setVisible(false);
			}
		}
	}

	@Override
	public HasClickHandlers getCollapsibleButton() {
		return controlButton;
	}



	@Override
	public boolean isCollapsibleCollapsed() {
		return collapsiblePanel.isCollapsed();
	}

	
}
